﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class WebMenuService : IWebMenuService
    {
        private IWebMenuManager _wm;
        public WebMenuService(IWebMenuManager wm)
        {
            _wm = wm;
        }

        public async Task<int> AddUsersAsync(string title, string link, string icon, int parentId)
        {
            return await _wm.AddAsync(new Model.WebMenu 
            {
                Title = title,
                Link = link,
                Icon = icon,
                ParentId = parentId
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _wm.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _wm.DeleteAsync(data);
        }

        public async Task<List<WebMenuDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _wm.GetChooseAsync(wm => wm.DeleteId == deleteId);
            return data == null ? new List<WebMenuDto>() : data.Select(wm => new WebMenuDto 
            {
                Id = wm.Id,
                Title = wm.Title,
                Link = wm.Link,
                Icon = wm.Icon,
                ParentId = wm.ParentId,
                CreateTime = wm.CreateTime,
                UpdateTime = wm.UpdateTime
            }).ToList();
        }

        public async Task<WebMenuDto> GetByIdAsync(int id)
        {
            var data = await _wm.GetById(id);
            return data == null ? new WebMenuDto() : new WebMenuDto 
            {
                Id = data.Id,
                Title = data.Title,
                Link = data.Link,
                Icon = data.Icon,
                ParentId = data.ParentId,
                DeleteId = data.DeleteId,
                CreateTime = data.CreateTime,
                UpdateTime= data.UpdateTime
            };
        }

        public async Task<List<WebMenuDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _wm.GetChooseAsync(wm => wm.Title.Contains(title) && wm.DeleteId == deleteId);
            return data == null ? new List<WebMenuDto>() : data.Select(wbm => new WebMenuDto 
            {
                Id = wbm.Id,
                Title = wbm.Title,
                Link = wbm.Link,
                Icon = wbm.Icon,
                ParentId = wbm.ParentId,
                DeleteId = wbm.DeleteId,
                CreateTime = wbm.CreateTime,
                UpdateTime = wbm.UpdateTime
            }).ToList();
        }

        public async Task<List<WebMenuDto>> GetByParentIdAsync(int parentId, int deleteId = 1)
        {
            var data = await _wm.GetChooseAsync(wm => wm.ParentId == parentId && wm.DeleteId == deleteId);
            return data == null ? new List<WebMenuDto>() : data.Select(wm => new WebMenuDto 
            {
                Id = wm.Id,
                Title = wm.Title,
                Link = wm.Link,
                Icon = wm.Icon,
                ParentId = wm.ParentId,
                CreateTime = wm.CreateTime,
                UpdateTime = wm.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _wm.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _wm.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _wm.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _wm.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string title, string link, string icon, int parentId)
        {
            var data = await _wm.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Link = link;
            data.Icon = icon;
            data.ParentId = parentId;
            data.UpdateTime = DateTime.Now;
            return await _wm.UpdateAsync(data);
        }
    }
}
